

import UIKit

protocol XSRegiestViewDelegate: NSObjectProtocol {
    
    func regiestBtnDidClick()
    func loginBtnDidClick()
}

class XSRegiestView: UIView {
    
    // MARK: - 属性
    weak var delegate: XSRegiestViewDelegate?///< 代理
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(textView)
        self.addSubview(regiestView)
        self.addSubview(loginView)
    }
    
    // MARK: - 添加动画
    func addAnimation()
    {
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        
        basic.toValue = M_PI * 2
        basic.repeatCount = MAXFLOAT
        basic.duration = 20
        basic.removedOnCompletion = false // 使动画跳转时不结束
        
        iconView.layer.addAnimation(basic, forKey: nil)
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        textView.translatesAutoresizingMaskIntoConstraints = false
        regiestView.translatesAutoresizingMaskIntoConstraints = false
        loginView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        self.addSubviewConstranint()
        
    }
    
    // MARK: - 添加约束
    private func addSubviewConstranint()
    {
        /// iconView 约束
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -50))
        
        /// homeView 约束
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /// textView 约束
        self.addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        self.addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        /// regiestView 约束
        self.addConstraint(NSLayoutConstraint(item: regiestView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: regiestView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        self.addConstraint(NSLayoutConstraint(item: regiestView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: regiestView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /// loginView 约束
        self.addConstraint(NSLayoutConstraint(item: loginView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        self.addConstraint(NSLayoutConstraint(item: loginView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /// coverView 约束
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: regiestView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    // MARK: - 懒加载
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var textView: UILabel = {
        
        let textView = UILabel()
        textView.text = "关注一些人，回这里看看有什么惊喜"
        textView.tintColor = UIColor.darkGrayColor()
        textView.font = UIFont.systemFontOfSize(15)
        textView.numberOfLines = 0
        
        return textView
    }()
    
    private lazy var regiestView: UIButton = {
        
        let regiestView = UIButton()
        
        regiestView.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        regiestView.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        regiestView.setTitle("注册", forState: UIControlState.Normal)
        regiestView.addTarget(self, action: "regiestDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return regiestView
    }()
    
    private lazy var loginView: UIButton = {
        
        let loginView = UIButton()
        
        loginView.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginView.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        loginView.setTitle("登陆", forState: UIControlState.Normal)
        loginView.addTarget(self, action: "loginDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginView
    }()
    
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // MARK: - 按钮点击事件
    // 私有按钮点击方法要是不想让别人调用，就要在前面加@objc 供系统识别
    @objc private func regiestDidClick()
    {
        self.delegate?.regiestBtnDidClick()
    }
    
    @objc private func loginDidClick()
    {
        self.delegate?.loginBtnDidClick()
    }
    
    // MARK: - 更换图片
    func changeRegiestView(imageName: String, title: String)
    {
        self.homeView.hidden = true
        self.iconView.image = UIImage(named: imageName)
        self.textView.text = title
    }
    
    // MARK: - 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    // MARK: - 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
}
